﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HQWeb.LibarySys
{
    public partial class MaterialInLibrary : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                if (Request["id"] != null)
                {
                    BindDate(Request["id"]);
                    hidLOMID.Value = Request["id"];
                }
            }
        }

        #region  绑定数据
        private void BindDate(string id)
        {
            int libraryOutMaterial_id = int.Parse(id);
            BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL lbll = new BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL();
            Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL lommodel = new Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL();
            lommodel = lbll.GetModel(libraryOutMaterial_id);
            if (lommodel.MATERIAL_STATE == "1")
            {
                Return_Num.Text = lommodel.RETURN_NUM.ToString();
                Material_Remark.Text = lommodel.REMARK;
                btnPass.Style["display"] = "none";
                Material_Remark.ReadOnly = true;
                Return_Num.ReadOnly = true;
            }
            hidMaterialID.Value = lommodel.MATERIAL_ID.ToString();
            Material_Name.Text = lommodel.MATERIAL_NAME;
            Materail_Num.Text = lommodel.NUM.ToString();
            string libraryOut_id = lommodel.LIBRARYOUTID;
            hidPid.Value = libraryOut_id;
            Model.LibrarySys.T_PUB_LIBRARYOUT libraryout = new Model.LibrarySys.T_PUB_LIBRARYOUT();
            BLL.LibrarySys.T_PUB_LIBRARYOUT bll = new BLL.LibrarySys.T_PUB_LIBRARYOUT();
            libraryout = bll.GetModel(libraryOut_id);
            int branch_id = int.Parse(libraryout.BRANCH_ID.ToString());
            BindBranchName(branch_id);
            int library_id = int.Parse(libraryout.LIBRARY_ID.ToString());
            hidLibraryID.Value = library_id.ToString();
            BindLibraryName(library_id);
            //Applications.Text = libraryout.NUM.ToString();
            //int material_id = int.Parse(libraryout.MATERIAL_ID.ToString());
            //BindMaterial(id);
            Applicants.Text = libraryout.APPLICANT_NAME;
            if (libraryout.APPLY_TIMEOUT != null)
            {
                begintime.Text = Convert.ToDateTime(libraryout.APPLY_TIMEOUT).ToShortDateString();
            }
            if (libraryout.PLAN_TIMEIN != null)
            {
                endtime.Text = Convert.ToDateTime(libraryout.PLAN_TIMEIN).ToShortDateString();
            }
            Remark.Text = libraryout.REMARK;
            Titel.Text = libraryout.TITEL;
            if (libraryout.APPROVER_NAME != null && libraryout.APPROVER_NAME != "")
            {
                Approver.Text = libraryout.APPROVER_NAME;
            }
            MyNotation.Text = libraryout.NOTATION;
        }
        #endregion

        #region 绑定材料
        private void BindMaterial(string id)
        {
            BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL bll = new BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL();
            //DataTable modelList = bll.GetListByPid(id).Tables[0];
        }
        #endregion

        #region 绑定仓库
        private void BindLibraryName(int library_id)
        {
            BLL.SysManage.T_PUB_LIBRARY bll = new BLL.SysManage.T_PUB_LIBRARY();
            Model.SysManage.T_PUB_LIBRARY library = bll.GetModel(library_id);
            if (library != null)
            {
                this.LibrayName.Text = library.LIBRARY_NAME;
                //this.LibraryID.Value = library.LIBRARY_ID.ToString();
            }
        }
        #endregion

        #region 绑定分店列表
        private void BindBranchName(int branch_id)
        {
            Model.SysManage.T_PUB_BRANCH branch = new Model.SysManage.T_PUB_BRANCH();
            BLL.SysManage.T_PUB_BRANCH LibraryBll = new BLL.SysManage.T_PUB_BRANCH();
            branch = LibraryBll.GetModel(branch_id);
            BranchName.Text = branch.BRANCH_NAME;
        }
        #endregion

        protected void btnPass_Click(object sender, EventArgs e)
        {
            int mnum = int.Parse(Materail_Num.Text);
            int rnum = int.Parse(Return_Num.Text);
            if (mnum == rnum)
            {
                BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL bll = new BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL();
                Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL lommodel = new Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL();
                BLL.LibrarySys.T_PUB_LIBRARYMATERIAL lbll = new BLL.LibrarySys.T_PUB_LIBRARYMATERIAL();
                Model.LibrarySys.T_PUB_LIBRARYMATERIAL model = new Model.LibrarySys.T_PUB_LIBRARYMATERIAL();
                int library_id = int.Parse(hidLibraryID.Value);
                int material_id =int.Parse(hidMaterialID.Value);
                model = lbll.GetModel(library_id, material_id);
                if (model != null)
                {
                    model.NUM += mnum;
                    int libraryoutmaterialID = int.Parse(hidLOMID.Value);
                    lommodel = bll.GetModel(libraryoutmaterialID);
                    lommodel.RETURN_NUM = rnum;
                    lommodel.REMARK = Material_Remark.Text.Trim();
                    lommodel.MATERIAL_STATE = "1";
                    lommodel.TIMEIN = DateTime.Now;
                    lbll.Update_Material(model, lommodel);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "入库" + lommodel.MATERIAL_NAME + " 数量为：" + rnum, DateTime.Now);
                    Response.Redirect("InLibrary.aspx");
                }
                else
                {
                    model = new Model.LibrarySys.T_PUB_LIBRARYMATERIAL();
                    model.LIBRARY_ID = library_id;
                    model.MATERIAL_ID = material_id;
                    model.NUM = rnum;
                    int libraryoutmaterialID = int.Parse(hidLOMID.Value);
                    lommodel = bll.GetModel(libraryoutmaterialID);
                    lommodel.RETURN_NUM = rnum;
                    lommodel.REMARK = Material_Remark.Text.Trim();
                    lommodel.MATERIAL_STATE = "1";
                    lommodel.TIMEIN = DateTime.Now;
                    //lommodel.DISCARD_ID = int.Parse(hidScrapID.Value);
                    lbll.Add_Material(model, lommodel);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "入库" + lommodel.MATERIAL_NAME + " 数量为：" + rnum, DateTime.Now);
                    Response.Redirect("InLibrary.aspx");
                }
            }
            else if (mnum >= rnum)
            {
                int strNum = mnum - rnum;
                int strID = int.Parse(hidLOMID.Value);
                //Universal.JS.Alert("出现物品遗失或损坏！请您填写报销单！");
                string strJS = "openScrap('" + strID + "','" + strNum + "')";
                Universal.JS.RunJS(strJS);
            }
            else
            {
                Universal.JS.Alert("您填写的归还数据不正确，可能是归还数量大于借出数量！请您重填！");
            }
        }

        protected void btn_scrap_click(object sender, EventArgs e)
        {
            //int mnum = int.Parse(Materail_Num.Text);
            int rnum = int.Parse(Return_Num.Text.Trim());
            BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL bll = new BLL.LibrarySys.T_PUB_LIBRARYOUTMATERIAL();
            Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL lommodel = new Model.LibrarySys.T_PUB_LIBRARYOUTMATERIAL();
            BLL.LibrarySys.T_PUB_LIBRARYMATERIAL lbll = new BLL.LibrarySys.T_PUB_LIBRARYMATERIAL();
            Model.LibrarySys.T_PUB_LIBRARYMATERIAL model = new Model.LibrarySys.T_PUB_LIBRARYMATERIAL();
            int library_id = int.Parse(hidLibraryID.Value);
            int material_id = int.Parse(hidMaterialID.Value);
            model = lbll.GetModel(library_id, material_id);
            if (model != null)
            {
                model.NUM += rnum;
                int libraryoutmaterialID = int.Parse(hidLOMID.Value);
                lommodel = bll.GetModel(libraryoutmaterialID);
                lommodel.RETURN_NUM = rnum;
                lommodel.REMARK = Material_Remark.Text.Trim();
                lommodel.MATERIAL_STATE = "1";
                lommodel.TIMEIN = DateTime.Now;
                lommodel.DISCARD_ID = int.Parse(hidScrapID.Value);
                lbll.Update_Material(model, lommodel);
                Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "入库" + lommodel.MATERIAL_NAME + " 数量为：" + rnum, DateTime.Now);
                Response.Redirect("InLibrary.aspx");
            }
            else
            {
                model = new Model.LibrarySys.T_PUB_LIBRARYMATERIAL();
                model.LIBRARY_ID = library_id;
                model.MATERIAL_ID = material_id;
                model.NUM = rnum;
                int libraryoutmaterialID = int.Parse(hidLOMID.Value);
                lommodel = bll.GetModel(libraryoutmaterialID);
                lommodel.RETURN_NUM = rnum;
                lommodel.REMARK = Material_Remark.Text.Trim();
                lommodel.MATERIAL_STATE = "1";
                lommodel.TIMEIN = DateTime.Now;
                lommodel.DISCARD_ID = int.Parse(hidScrapID.Value);
                lbll.Add_Material(model, lommodel);
                Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "入库" + lommodel.MATERIAL_NAME + " 数量为：" + rnum, DateTime.Now);
                Response.Redirect("InLibrary.aspx");
            }
        }

        protected void btnReturnA_Click(object sender, EventArgs e)
        {
            Response.Redirect("InLibrary.aspx");
        }
    }
}